# salience and importance  ----------------------------------------------------
# abs_discussion
abs_discussion_el <- ols_main(
	outcome = "abs_discussion",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

abs_discussion_el_pvals <- get_RI_pvals(
	outcome = "abs_discussion",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

abs_discussion_ml <- ols_main(
	outcome = "abs_discussion_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

abs_discussion_ml_pvals <- get_RI_pvals(
	outcome = "abs_discussion_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# abs_goal
abs_goal_el <- ols_main(
	outcome = "abs_goal",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

abs_goal_el_pvals <- get_RI_pvals(
	outcome = "abs_goal",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

abs_goal_ml <- ols_main(
	outcome = "abs_goal_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

abs_goal_ml_pvals <- get_RI_pvals(
	outcome = "abs_goal_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# more_schools
more_schools_el <- ols_main(
	outcome = "more_schools",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

more_schools_el_pvals <- get_RI_pvals(
	outcome = "more_schools",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

more_schools_ml <- ols_main(
	outcome = "more_schools_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

more_schools_ml_pvals <- get_RI_pvals(
	outcome = "more_schools_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# candidate #controlling  for other assigned characteristics of candidates
candidate <- ols_main(
	outcome = "candidate",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = c("name_educ", "non_educ_policy"),
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

candidate_pvals <- get_RI_pvals(
	outcome = "candidate",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = c("name_educ","non_educ_policy"), 
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

#setup
control_means <- with(
	el, 
	c(
		"Control Mean",
		round(mean(abs_discussion_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(abs_discussion[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(abs_goal_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(abs_goal[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(more_schools_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(more_schools[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(candidate[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2)
	)
)



pval_lines <- c(
	"RI $p$-values",
	round(abs_discussion_ml_pvals$ri_pvals["absenteeism"],3),
	round(abs_discussion_el_pvals$ri_pvals["absenteeism"],3),
	round(abs_goal_ml_pvals$ri_pvals["absenteeism"],3),
	round(abs_goal_el_pvals$ri_pvals["absenteeism"],3),
	round(more_schools_ml_pvals$ri_pvals["absenteeism"],3),
	round(more_schools_el_pvals$ri_pvals["absenteeism"],3),
	round(candidate_pvals$ri_pvals["absenteeism"],3)
)

hypothesis_lines <- c(
	"Hypothesis",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr"
)

#make tables
sink("03_tables/ABS_salience_and_importance_test.tex")
stargazer(
	... = list(
		abs_discussion_ml$fit,
		abs_discussion_el$fit,
		abs_goal_ml$fit,
		abs_goal_el$fit,
		more_schools_ml$fit,
		more_schools_el$fit,
		candidate$fit
	),
	type = "latex",
	p = list(
		abs_discussion_ml_pvals$ri_pvals,
		abs_discussion_el_pvals$ri_pvals,
		abs_goal_ml_pvals$ri_pvals,
		abs_goal_el_pvals$ri_pvals,
		more_schools_ml_pvals$ri_pvals,
		more_schools_el_pvals$ri_pvals,
		candidate$ri_pvals
	),
	se = list(
		abs_discussion_ml$fit_summary[,"Std. Error"],
		abs_discussion_el$fit_summary[,"Std. Error"],
		abs_goal_ml$fit_summary[,"Std. Error"],
		abs_goal_el$fit_summary[,"Std. Error"],
		more_schools_ml$fit_summary[,"Std. Error"],
		more_schools_el$fit_summary[,"Std. Error"],
		candidate$fit_summary[,"Std. Error"]
	),
	keep = "absenteeism",
	omit.stat = c("rsq","f","ser"),
	column.separate = c(2,2,2,1),
	column.labels = c("Discussed absenteeism","Teachers/absenteeism important","Schools important","Candidate platform"),
	table.layout = "=cd#-t-as=n",
	dep.var.labels = c("Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline",
										 "Endline"
	),
	dep.var.labels.include = TRUE,
	no.space = T,
	omit = "block_id",
	add.lines = list(
		control_means,
		pval_lines,
		hypothesis_lines,
		c("Block FE",
			"Yes","Yes","Yes",
			"Yes","Yes","Yes", 
			"Yes", "Yes","Yes")),
	notes.label = "",
	float = FALSE
	# style = "qje"
)
sink()
